package cn.eweb.ttkt;

import cn.eweb.util.Tools;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings.PluginState;

public class VideoActivity extends Activity {

	private WebView webView;
	private View myView = null;
	private CustomViewCallback myCallback = null;	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		webView = (WebView) this.findViewById(R.id.wv);
		webView.getSettings().setPluginState(PluginState.ON);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		// 创建WebViewClient对象
		WebViewClient wvc = new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器加载页面
				if(Tools.isNetWorkAvailable(getApplicationContext())){
					view.loadUrl(url);
				}else{
					view.loadUrl(getString(R.string.video_url));
				}
				// 记得消耗掉这个事件。给不知道的朋友再解释一下，Android中返回True的意思就是到此为止吧,事件就会不会冒泡传递了，我们称之为消耗掉
				return true;
			}
		};
		webView.setWebViewClient(wvc);
		webView.setWebChromeClient(new WebChromeClient() {		

			@Override
			public void onShowCustomView(View view, CustomViewCallback callback) {
				if (myCallback != null) {
					myCallback.onCustomViewHidden();
					myCallback = null;
					return;
				}

				ViewGroup parent = (ViewGroup) webView.getParent();
				parent.removeView(webView);
				view.setBackgroundColor(Color.BLACK);
				Log.e("test",view.getClass().getName());
				parent.addView(view);
				myView = view;
				myCallback = callback;
//				chromeClient = this;
			}

			@Override
			public void onHideCustomView() {

				if (myView != null) {
					if (myCallback != null) {
						myCallback.onCustomViewHidden();
						myCallback = null;
					}
					ViewGroup parent = (ViewGroup) myView.getParent();
					parent.removeView(myView);
					parent.addView(webView);
					myView = null;
				}
			}
		});
		if(Tools.isNetWorkAvailable(getApplicationContext())){
			webView.loadUrl(getString(R.string.video_url));
		}else{
        	webView.loadUrl(getString(R.string.error_url));
        }
	}

	@Override
	protected void onPause() {
		webView.onPause();
		if (myView != null) {
			if (myCallback != null) {
				myCallback.onCustomViewHidden();
				myCallback = null;
			}
			ViewGroup parent = (ViewGroup) myView.getParent();
			parent.removeView(myView);
			parent.addView(webView);
			myView = null;
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		webView.onResume();
		if(webView.getOriginalUrl()!=null){
			if(Tools.isNetWorkAvailable(getApplicationContext())&&webView.getOriginalUrl().equals(getString(R.string.error_url))){
				webView.loadUrl(getString(R.string.video_url)); 
			}
		}
		super.onResume();
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
			if(webView.canGoBack()){
				webView.goBack(); //goBack()表示返回WebView的上一页面 
	            return true; 
			}else{
				AlertDialog.Builder ad = new AlertDialog.Builder(this);
	        	ad.setIcon(android.R.drawable.ic_dialog_info);
	    		ad.setTitle(getString(R.string.quit)); // 设置标题 
	            ad.setMessage(getString(R.string.quit_confirm));
	            ad.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

	    			public void onClick(DialogInterface dialog, int which) {
	    				dialog.cancel();
	    				VideoActivity.this.finish();
	    			} 
	            });  
	            ad.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {

	    			public void onClick(DialogInterface dialog, int which) {
	    				dialog.cancel();
	    			}
	                   
	            });  
	            AlertDialog alert = ad.create();
	            alert.show();
			}
        }
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		/*
         * 
         * add()方法的四个参数，依次是：
         * 1、组别，如果不分组的话就写Menu.NONE,
         * 2、Id，这个很重要，Android根据这个Id来确定不同的菜单
         * 3、顺序，那个菜单现在在前面由这个参数的大小决定
         * 4、文本，菜单的显示文本
         */
        menu.add(Menu.NONE, Menu.FIRST + 1, 1, getString(R.string.quit)).setIcon(
        		android.R.drawable.ic_menu_revert);
        return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case Menu.FIRST + 1:
        	VideoActivity.this.finish();
            break;
		}
		return false;
	}
	
}
